﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace SharpDX
{
    public class CGraphic
    {
        public CD3D d3d;
        public CCamera camera;
        public CModel model;
        public CTextureShader textureShader;
        public bool Initialize(IntPtr hwnd,int scW,int scH)
        {
            d3d = new CD3D();
            if(!d3d.Initialize(hwnd,scW,scH))
            {
                return false;
            }
            camera = new CCamera();
            camera.setPos(0, 0, 10);
            model = new CModel();
            if (!model.Initialize(d3d.m_device, "seafloor.dds"))
                return false;

            if (!textureShader.Initialize(d3d.m_device, hwnd))
                return false;

            return true;
        }
        public void Shutdown()
        {
            // Release the color shader object.
            if (textureShader != null)
            {
                textureShader.ShutDown();
                textureShader = null;
            }

            // Release the model object.
            if (model != null)
            {
                model.Shutdown();
                model = null;
            }

            // Release the camera object.
            if (camera != null)
            {
                camera = null;
            }

            // Release the Direct3D object.
            if (d3d != null)
            {
                d3d.ShutDown();
                d3d = null;
            }
        }
        public void Render()
        {
            d3d.BeginScene(new Color4(0f,0f,0f,1f));

            camera.Render();
            var viewMat = camera.viewMat;
            var worldMat = d3d.m_worldMat;
            var projectionMat = d3d.m_projectionMat;

            model.Render(d3d.m_devicecontext);
            textureShader.Render(d3d.m_devicecontext, model.indexCount, worldMat, viewMat, projectionMat, model.texture.m_textureResource);
            d3d.EndScene();
        }
    }
}
